package com.infoland.afanmall.model.zhongChou.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.zhongChou.activity.ZhongchouInfoActivity;
import com.infoland.afanmall.model.zhongChou.adapter.ListFragmentAdapter;
import com.infoland.afanmall.model.zhongChou.entity.ZhongChouResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * @author 王永吉
 * @date 2018/7/12 9:23
 */
public class ListFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.zhong_chou_recycle)
    RecyclerView recyclerView;
    SmartRefreshLayout smartRefreshLayout;
    AVLoadingIndicatorView aviPt;
    private int activityId = 0;
    private int orderBy = 0;
    private String name = "";
    private int NOW_PAGE = 1;
    private boolean IS_LAST = false;
    private ListFragmentAdapter listFragmentAdapter;

    public void setSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout) {
        this.smartRefreshLayout = smartRefreshLayout;
    }

    private int getLayout() {
        return R.layout.zhong_chou_fragment;
    }

    public void initData() {

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listFragmentAdapter = new ListFragmentAdapter(R.layout.zhong_chou_item, new ArrayList<>());
        recyclerView.setAdapter(listFragmentAdapter);
        listFragmentAdapter.setZhongChouListener((int adapterPosition) -> {
            LogKit.E("adapterPosition ---->   ", "  " + adapterPosition);
            ToolsKit.goDh(getActivity(), ZhongchouInfoActivity.class);
        });
    }

    //初始化 和刷新
    public void initZhongChou() {
        JSONObject para = getPage(CONST.ZhongChou.INIT_SIZE, 1);
        JSONObject params = new JSONObject();
        params.put("pageNumber", 1);
        params.put("pageSize", 10);
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("initZhongChou ---->   ", "  " + response200);
                    ZhongChouResponse zhongChouResponse = JSONObject.parseObject(response200, ZhongChouResponse.class);
                    if (zhongChouResponse.isSuccess()) {
                        ZhongChouResponse.ObjectBean object = zhongChouResponse.getObject();
                        NOW_PAGE = object.getPageNumber();
                        IS_LAST = object.isLastPage();
                        initAdapter(object.getList());
                    } else {
                    }
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.ZhongChou.URL_GET_LIST, para, getActivity());
    }

    // 获取分页参数
    private JSONObject getPage(int pageSize, int pageNumber) {
        JSONObject para = new JSONObject();
        para.put("pageSize", pageSize);
        para.put("pageNumber", pageNumber);
        para.put("activityId", this.activityId);
        para.put("orderBy", this.orderBy);
        return para;
    }

    //初始化 和刷新
    private void initAdapter(List<ZhongChouResponse.ObjectBean.ListBean> list) {
        listFragmentAdapter.setNewData(list);
        listFragmentAdapter.loadMoreComplete();
        recyclerView.scrollToPosition(0);
        loadOk.loadIsOk(false);
    }

    //加载 数据
    public void loadZhongChou() {
        if (IS_LAST) {
            loadOk.loadIsOk(true);
        } else {

            new OkHttpKit().send(
                    response200 -> {
                        ZhongChouResponse zhongChouResponse = JSONObject.parseObject(response200, ZhongChouResponse.class);
                        if (zhongChouResponse.isSuccess()) {
                            ZhongChouResponse.ObjectBean object = zhongChouResponse.getObject();
                            NOW_PAGE = object.getPageNumber();
                            IS_LAST = object.isLastPage();
                            loadAdapter(object.getList());
                        } else {

                        }
                    },
                    success401 -> {
                        LogKit.E("success401 ---->   ", "  " + success401);
                    },
                    error -> {
                        LogKit.E("error ---->   ", "  " + error);
                    }
            ).post(CONST.ZhongChou.URL_GET_LIST, getPage(CONST.ZhongChou.LOAD_SIZE, (NOW_PAGE + 1)), getActivity());

        }
    }

    //加载 数据
    public void loadAdapter(List<ZhongChouResponse.ObjectBean.ListBean> list) {
        LogKit.E("aaaa ---->   ", " list ");
        loadOk.loadIsOk(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayout(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            this.activityId = getArguments().getInt("activityId");
            this.orderBy = getArguments().getInt("orderBy");
            this.name = getArguments().getString("name");
        }
        initData();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    LoadOk loadOk;
    refOk refOk;

    public void setLoadOk(LoadOk loadOk) {
        this.loadOk = loadOk;
    }

    public void setRefOk(ListFragment.refOk refOk) {
        this.refOk = refOk;
    }

    public interface LoadOk {
        void loadIsOk(Boolean IS_LAST);
    }

    public interface refOk {
        void refIsOk(Boolean IS_LAST);
    }

    @Override
    public void onDestroy() {
        ToolsKit.deleteOKHttp(getActivity());
        super.onDestroy();
    }
}
